import {login, encrypt, decrypt} from './src/main.js';

import './index.css'; // 需要在js文件中导入css文件，打包后才有样式

export function submitAction() {
    let name = document.getElementById('name').value;
    if (isEmpty(name)) {
        alert('请输入用户名');
        return;
    }
    let password = document.getElementById('password').value;
    if (isEmpty(password)) {
        alert('请输入密码');
        return;
    }

    console.log(`name: ${name}, password:${password}`);

    let t = login(password, name);
    if (!isEmpty(t)) {
        document.getElementById('token').innerText = t;

        let en_name =  encrypt(name);
        let en_pwd = encrypt(password);
        
        document.getElementById('encrypt_name').innerText = en_name;
        document.getElementById('encrypt_password').innerText = en_pwd;

        document.getElementById('encrypt_name_back').innerText = decrypt(en_name);
        document.getElementById('encrypt_password_back').innerText = decrypt(en_pwd);
    }
}

export function isEmpty(val) {
    return val == undefined || val == null || val == "";
}

// 将事件绑定放在js文件中处理，避免html中识别不到事件的问题
document.addEventListener('DOMContentLoaded', () => {
    const button = document.getElementById('submit_button');
    if (button) {
        button.addEventListener('click', submitAction);
    }
});
